package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.PurchaseListGoodsMapper;
import com.atguigu.jxc.dao.PurchaseListMapper;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jxc
 * @description: 商品报表统计
 * @author: ZJH
 * @create: 2023-02-11 09:56
 **/
@Service
@SuppressWarnings("all")
public class PurchaseListServiceIml implements PurchaseListService {

    @Autowired
    private PurchaseListMapper purchaseListMapper;
    @Autowired
    private PurchaseListGoodsMapper purchaseListGoodsMapper;
    //支付结算（修改进货单付款状态）
    @Override
    public void updateState(Integer purchaseListId) {
        PurchaseList purchaseList = new PurchaseList();
        purchaseList.setPurchaseListId(purchaseListId);
        purchaseList.setState(1);
        purchaseListMapper.updateById(purchaseList);
    }

    @Override
    public  List<Map<String,Object>> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
       List<Map<String,Object>> list = new ArrayList<>();
       List<PurchaseList> lists =  purchaseListMapper.getPurchaseList(sTime,eTime);
        for (PurchaseList purchaseList : lists) {
            Integer purchaseListId = purchaseList.getPurchaseListId();
           List<PurchaseListGoods> listGoodsList = purchaseListGoodsMapper.getPurListGoods(purchaseListId,goodsTypeId,codeOrName,goodsTypeId);
            for (PurchaseListGoods purchaseListGoods : listGoodsList) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("number",purchaseList.getPurchaseNumber());
                map.put("date",purchaseList.getPurchaseDate());
                map.put("supplierName",purchaseList.getSupplierName());
                map.put("code",purchaseListGoods.getGoodsCode());
                map.put("name",purchaseListGoods.getGoodsName());
                map.put("model",purchaseListGoods.getGoodsModel());
                map.put("goodsType",purchaseListGoods.getGoodsTypeName());
                map.put("unit",purchaseListGoods.getGoodsUnit());
                map.put("price",purchaseListGoods.getPrice());
                map.put("num",purchaseListGoods.getGoodsNum());
                map.put("total",purchaseListGoods.getTotal());
                list.add(map);
            }
        }
       return list;
    }

    //保存进货单
    @Override
    public void insert(PurchaseList purchaseList, PurchaseListGoods purchaseListGoods) {
        purchaseListMapper.insert(purchaseList);
        purchaseListGoodsMapper.insert(purchaseListGoods);
    }


}
